package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GButton;
import g4p_controls.GEditableTextControl;
import g4p_controls.GEvent;
import g4p_controls.GPassword;
import g4p_controls.GTabManager;
import g4p_controls.GTextArea;
import g4p_controls.GTextField;
import processing.core.PApplet;


public class TabToTextControl_Test extends PApplet {

	GTextField txf1, txf2;
	GPassword pwd1;
	GTextArea txa1, txa2;
	GButton bf1, bf2, ba1, ba2;
	GButton appendTxa2, appendTxf2;
	GTabManager tt;
	String s;

	public void setup(){
		size(600,400);

		tt = new GTabManager();

		txf1 = new GTextField(this, 80,20,200,20);
		txf1.tag = "txf1";
		txf1.setPromptText("Text field 1");

		txf2 = new GTextField(this, 380,20,200,30, G4P.SCROLLBARS_HORIZONTAL_ONLY);
		txf2.tag = "txf2";
		txf2.setPromptText("Text field 2");
		s = "Peter Lager is a nice guy who lives in Rhyl";
		txf2.setText(s);

		pwd1 = new GPassword(this, 380,60,200,30, G4P.SCROLLBARS_HORIZONTAL_ONLY);
		pwd1.tag = "pwd1";

		txa1 = new GTextArea(this, 80,60,200,120);
		txa1.tag = "txa1";
		txa1.setPromptText("Text area 1");
		s = "Peter Lager Lives\nin Rhyl";
		//		txa1.setText(s, 300);

		txa2 = new GTextArea(this, 380,100,200,120, G4P.SCROLLBARS_BOTH);
		txa2.tag = "txa2";
		txa2.setPromptText("Text area 2");
		String[] paragraphs = loadStrings("g4p/book3.txt");
		s = PApplet.join(paragraphs, '\n');
		txa2.setText(s, 300);

		bf1 = new GButton(this, 20,20,20,20,"");
		bf2 = new GButton(this, 320,20,20,20,"");
		ba1 = new GButton(this, 20,60,20,20,"");
		ba2 = new GButton(this, 320,100,20,20,"");

		tt.addControls(txf1, txa1, txf2, pwd1, txa2);

		appendTxf2 = new GButton(this, 20,260,200,20,"Append to text field");
		appendTxa2 = new GButton(this, 20,290,200,20,"Append to text area");

		txa1.setFocus(false);

		//		G4P.setMouseOverEnabled(true);
	}

	public void displayEvent(String name, GEvent event){
		String extra = " event fired at " + millis() / 1000.0 + "s";
		print(name + "   ");
		switch(event){
		case CHANGED:
			println("CHANGED " + extra);
			break;
		case SELECTION_CHANGED:
			println("SELECTION_CHANGED " + extra);
			break;
		case LOST_FOCUS:
			println("LOST_FOCUS " + extra);
			break;
		case GETS_FOCUS:
			println("GETS_FOCUS " + extra);
			break;
		case ENTERED:
			println("ENTERED " + extra);	
			break;
		default:
			println("UNKNOWN " + extra);
		}
	}
	
	public void handleTextEvents(GEditableTextControl textControl, GEvent event) { 
		displayEvent(textControl.tag, event);
	}

	public void handlePasswordEvents(GPassword pwordControl, GEvent event) {
		displayEvent(pwordControl.tag, event);
	}


	public void handleButtonEvents(GButton button, GEvent event) {
		if(button == bf1)
			txf1.setFocus(true);
		if(button == bf2)
			txf2.setFocus(true);
		if(button == ba1)
			txa1.setFocus(true);
		if(button == ba2)
			txa2.setFocus(true);
		if(button == appendTxa2){
			txa1.appendText("Mary\n   had\n    a little lamb");
			txa2.appendText("Mary\n   had\n    a little lamb");
		}
		if(button == appendTxf2){
			txf1.appendText("Mary had a little lamb");
			txf2.appendText("Mary had a little lamb");
		}
	}

	public void draw(){
		background(240);
	}
}
